package com.huiyeji.hui.controller;

import com.huiyeji.hui.auth.Authenticator;
import com.huiyeji.hui.auth.License;
import com.huiyeji.hui.entity.User;
import com.huiyeji.hui.service.ApplicationService;
import com.huiyeji.hui.utils.HuiResponse;
import com.huiyeji.hui.utils.SuccessEnum;
import com.huiyeji.hui.utils.ValidateUtil;
import com.huiyeji.hui.vo.PageResult;
import com.huiyeji.hui.vo.PwdChangeVo;
import com.huiyeji.hui.vo.SelfAndOthersAndGroupsVo;
import com.huiyeji.hui.vo.SimpleOtherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @author zhaoduo
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final ApplicationService service;

    @Autowired
    public UserController(ApplicationService service){
        this.service = service;
    }

    private final Authenticator authenticator = Authenticator.getInstance();

    @GetMapping("/getOther/{oid}")
    public HuiResponse<?> getOther(HttpSession session,
                                   @PathVariable("oid")Long oid){
        User user = authenticator.check(session, License.GENERAL);
        SelfAndOthersAndGroupsVo.Other other = service.getOther(user.getUid(),oid);
        return HuiResponse.ok(other);
    }

    @GetMapping("/getPolicy")
    public HuiResponse<?> getPolicy(HttpSession session){
        authenticator.check(session,License.GENERAL);
        Map<String,String> map = service.getPolicy();
        return HuiResponse.ok(map);
    }

    @PostMapping("/baseModify")
    public HuiResponse<?> baseModify(@RequestBody User requestUser, HttpSession session) throws CloneNotSupportedException {
        User user = authenticator.check(session, License.GENERAL);
        requestUser.setUid(user.getUid());
        return service.userBaseModify(requestUser,user);
    }

    @PostMapping("/changePwd")
    public HuiResponse<?> changePwd(@Validated PwdChangeVo vo,
                                    BindingResult bindingResult,
                                    HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        ValidateUtil.analyzeBindingResult(bindingResult);
        vo.check();
        service.changeUserPwd(user,vo);

        //修改成功,销毁session,前端让用户重新登录
        session.invalidate();

        return HuiResponse.ok(SuccessEnum.MODIFY_SUCCESS);
    }

    @GetMapping("/getSelfAndOthersAndGroups")
    public HuiResponse<?> getFriendsAndGroups(HttpSession session) throws ExecutionException, InterruptedException, CloneNotSupportedException {
        User user = authenticator.check(session, License.GENERAL);
        SelfAndOthersAndGroupsVo vo = service.getSelfAndOthersAndGroups(user);
        return HuiResponse.ok(vo);
    }

    @PostMapping("/changeHeadshot")
    public HuiResponse<?> changeHeadshot(HttpSession session,
                                         @RequestParam("headshot")String headshot){
        User user = authenticator.check(session, License.GENERAL);
        if (!user.getHeadshot().equals(headshot)){
            service.changeHeadshot(user,headshot);
        }
        return HuiResponse.ok();
    }

    @GetMapping("/keySearch")
    public HuiResponse<?> keySearch(HttpSession session,
                                    @RequestParam Map<String ,String> params){
        User user = authenticator.check(session, License.GENERAL);
        PageResult<SimpleOtherVo> result = service.userKeySearch(user.getUid(), params);
        return HuiResponse.ok(result);
    }

    @GetMapping("/conditionSearch")
    public HuiResponse<?> conditionSearch(HttpSession session,
                                          @RequestParam Map<String ,String> params){
        User user = authenticator.check(session, License.GENERAL);
        PageResult<SimpleOtherVo> result = service.userConditionSearch(user.getUid(), params);
        return HuiResponse.ok(result);
    }
}
